package com.test.tool.demo.websocket;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.Serializable;

/**
 * @author 小八
 * @date 2022年04月25日15:54
 */
public class ResponseWrapper<T> implements Serializable {
    private Integer busiCode;
    private String msg;
    private T data;

    public static <T> ResponseWrapper<T> ok(T r) {
        ResponseWrapper wrapper = new ResponseWrapper();
        wrapper.setBusiCode(0);
        wrapper.setMsg("请求成功");
        wrapper.setData(r);
        return wrapper;
    }

    public static <T> ResponseWrapper<T> ok() {
        ResponseWrapper wrapper = new ResponseWrapper();
        wrapper.setBusiCode(0);
        wrapper.setMsg("请求成功");
        wrapper.setData(null);
        return wrapper;
    }

    public static ResponseWrapper error(Integer busiCode, String msg) {
        ResponseWrapper wrapper = new ResponseWrapper();
        wrapper.setBusiCode(busiCode);
        wrapper.setMsg(msg);
        return wrapper;
    }

    public static ResponseWrapper error(Integer busiCode, String msg, Throwable cause) {
        ResponseWrapper wrapper = new ResponseWrapper();
        wrapper.setBusiCode(busiCode);
        wrapper.setMsg(msg);
        return wrapper;
    }

    public ResponseWrapper(Integer busiCode, String msg) {
        this.busiCode = busiCode;
        this.msg = msg;
    }
    public ResponseWrapper() {}


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getBusiCode() {
        return busiCode;
    }

    public void setBusiCode(Integer busiCode) {
        this.busiCode = busiCode;
    }

    public T getData() {
        return data;
    }

    public <T> T getData(Class<T> clazz) {
        T t = JSONUtil.toBean(new JSONObject(data), clazz, false);
        return t;
    }

    public void setData(T data) {
        this.data = data;
    }
}
